var config = require("config.js");
function ajax(url,data,s,f){
  wx.request({
    url: url,
    data:data,
    method: "post",
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
  
      s&&s(res);
    },
    fail:function(res){
      f&&f(res)
    }
  })
} 

function phoneVer(phone){
  var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
  if (!phoneReg.test(phone)) {
    wx.showToast({
      title: '手机号码有误',
      image: '/images/closered.png',
    })
    wx.hideLoading()
  }
  return;
}
function loginVer(that, cb) {
  var userinfo = wx.getStorageSync('userinfo');
  if (!userinfo) {
    wx.redirectTo({
      url: '/pages/login/login',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  }else{
    typeof cb == 'function' && cb(that);
  }
  return;
}


//输入e和要获取的键返回键对应的值
function getdata(e){
 return e.currentTarget.dataset
}
function getTemImg(that, imgname = 'imgname', i = 0) {
  wx.chooseImage({
    count: 1, // 默认9
    success: (res) => {
      var i = 0; //第几个
      var img_num = res.tempFiles.length;
      wx.uploadFile({
        url: config.hostapi + 'UpFile/FileUpImg',
        filePath: res.tempFilePaths[i],
        name: 'fileData',
        pathname: 'sendpet',
        success: (res) => {
          var data = JSON.parse(res.data.replace(/^\s+|\s+$/g, ""));
          var name = data.data;
          that.setData({[imgname]:name });
        }
      });
    },
  });
}

//星座检测输入年月日格式为2011-11-12
function checkxingzuo(a) {
  var arr = a.split("-")
  switch (arr[1]*1) {
    case 1:
      return arr[2] <= 19 ? "摩羯座" : "水瓶座";
      break;
    case 2:
      return arr[2] <= 18 ? "水瓶座" : "双鱼座";
      break;
    case 3:
      return arr[2] <= 20 ? "双鱼座" : "白羊座";
      break;
    case 4:
      return arr[2] <= 19 ? "白羊座" : "金牛座";
      break;
    case 5:
      return arr[2] <= 20 ? "金牛座" : "双子座";
      break;
    case 6:
      return arr[2] <= 21 ? "双子座" : "巨蟹座";
      break;
    case 7:
      return arr[2] <= 22 ? "巨蟹座" : "狮子座";
      break;
    case 8:
      return arr[2] <= 22 ? "狮子座" : "处女座";
      break;
    case 9:
      return arr[2] <= 23 ? "处女座" : "天秤座";
      break;
    case 10:
      return arr[2] <= 23 ? "天秤座" : "天蝎座";
      break;
    case 11:
      return arr[2] <= 22 ? "天蝎座" : "射手座";
      break;
    case 12:
      return arr[2] <= 21 ? "射手座" : "摩羯座";
      break;
  }
}
function uinfo(that){
  var uinfo=wx.getStorageSync('userinfo');
  that.setData({ uinfo: uinfo});
}
function stararray($num) {
  var array = [];
  for (var i = 0; i < $num; i++) {
    array[i] = 1;
  }
  return array;
}
/**
 * 消息时间
 */
function getDateDiff(dateTimeStamp) {
  var minute = 1000 * 60;
  var hour = minute * 60;
  var day = hour * 24;
  var halfamonth = day * 15;
  var month = day * 30;
  var now = new Date().getTime();
  var diffValue = now - dateTimeStamp;
  if (diffValue < 0) { return; }
  var monthC = diffValue / month;
  var weekC = diffValue / (7 * day);
  var dayC = diffValue / day;
  var hourC = diffValue / hour;
  var minC = diffValue / minute;
  var result;
  if (monthC >= 1) {
    result = "" + parseInt(monthC) + "月前";
  }
  else if (weekC >= 1) {
    result = "" + parseInt(weekC) + "周前";
  }
  else if (dayC >= 1) {
    result = "" + parseInt(dayC) + "天前";
  }
  else if (hourC >= 1) {
    result = "" + parseInt(hourC) + "小时前";
  }
  else if (minC >= 1) {
    result = "" + parseInt(minC) + "分钟前";
  } else
    result = "刚刚";
  return result;
}

function pan(warn,status=1){
  if (status==1){
    wx.showToast({
      title: warn,
      image: '/images/dui.png'
    })   
  }else if(status == 0){
    wx.showToast({
      title: warn,
      image: '/images/closered.png'
    }) 
  }
  
}
 //倒计时从新运行
function reRun(funcname, that, time, cb) {
  that.setData({
    flag: time
  })
 var timer = setInterval(() => {
   var reRun = wx.getStorageSync(funcname+'reRun');//获取reRun缓存
    if (reRun==1){//如果外界设置了1 则表明 此循环需要终止
      clearInterval(timer);
      that.setData({
        flag: time
      })
      console.log(reRun);
      console.log(funcname + 'reRun');
      
      wx.setStorageSync(funcname+'reRun', ' ');
      return;
    }
    var inttime = that.data.flag;
    console.log(funcname + '倒计时开始:' + inttime+'  ' + 'reRunName=' + funcname + '  '+'reRun 值=' + reRun);
    //规定时间内 内有被终止 需要从新运行一下方法 倒计时需要格式化
    if (inttime == 0) {
      that.setData({
        flag: time
      })
      //回调函数从新运行一遍 自己
      typeof cb == 'function' && cb(that);
      return;
    }
    else {
      that.setData({
        flag: that.data.flag - 1
      })
    }
  }, 1000)
} 

function loginVertest(that, cb) {
  var userinfo = wx.getStorageSync('userinfo');
  if (!userinfo) {
    wx.redirectTo({
      url: '/pages/login/login',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  } else {
    typeof cb == 'function' && cb(that);
  }
  return;
}

module.exports={
  pan: pan,
  ajax:ajax,
  checkxingzuo:checkxingzuo,
  getdata:getdata,
  phoneVer: phoneVer,
  loginVer: loginVer,
  uinfo: uinfo,
  getTemImg: getTemImg,
  getDateDiff: getDateDiff,
  stararray: stararray,
  reRun: reRun
}